import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
//  _MyHomePageState createState() => _MyHomePageState();
//  _RowColumnState createState() => _RowColumnState();
//  _ContainerState createState() => _ContainerState();
//  _GridViewState createState() => _GridViewState();
//  _ListViewState createState() => _ListViewState();
  _StackState createState() => _StackState();
}

class _StackState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    var stack = Stack(
      alignment: const Alignment(0.0, 0.55),
      children: [
        CircleAvatar(
          backgroundImage: AssetImage('images/002.png'),
          radius: 180.0,
        ),
        Container(
          decoration: BoxDecoration(
            color: Colors.black45,
          ),
          child: Text(
            'Star Sky',
            style: TextStyle(
                fontSize: 20.0,
                fontWeight: FontWeight.bold,
                color: Colors.white),
          ),
        ),
      ],
    );

    return Scaffold(
      appBar: AppBar(
        title: Text('Stack 示例'),
      ),
      body: Align(
        alignment: FractionalOffset(0.5, 0.1),
        child: stack,
      ),
    );
  }
}

//ListView 使用示例 系统默认cell 以及点击、长按事件的添加
ListTile getListTitLeWithInfo(String title, String subTitle, int index) {
  const titleStyle = TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0);
  return ListTile(
    title: Text(
      title,
      style: titleStyle,
    ),
    subtitle: Text(subTitle),
    leading: Icon(
      Icons.theaters,
      color: Colors.blue[500],
    ),
    trailing: Icon(
      Icons.navigate_next,
      color: Colors.grey[400],
    ),
    onTap: () => listTileTapAction(index),
    onLongPress: () => listTileLongPressAction(index),
  );
}

void listTileTapAction(int index) {
  print('listTile on Clicking!!  ++++++   $index');
}

void listTileLongPressAction(int index) {
  print('listTile on LongPress!!  ++++++   $index');
}

/*
* 手动搭建列表 cell
* count 行数目
* */
List<Widget> getListWithCount(int count) {
  List<Widget> list = [];
  const divider = Divider(
    height: 1,
    indent: 15.0,
    color: Colors.black,
  );
  for (var i = 0; i < count; i++) {
    list.add(getListTitLeWithInfo(
        'CineArts at the Empire [${i + 1}]', '85 W Portal Ave [${i + 1}]', i));
    list.add(divider);
  }
  return list;
}

/*
* 使用系统自带的生成方法搭建列表 cell
* list 传入模拟模型数组
* */
Widget buildListViewUseBuilder(List<TestPerModel> list) {
  return ListView.builder(
//    滑动方向
      scrollDirection: Axis.vertical,
      padding: EdgeInsets.all(10.0),
      reverse: false,
      primary: true,
      itemExtent: 50.0,
      shrinkWrap: true,
      itemCount: list.length,
      cacheExtent: 30.0,
      physics: ClampingScrollPhysics(),
      itemBuilder: (context, i) => Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Text(
                  list[i].name,
                  style: TextStyle(fontSize: 22.0, color: Colors.red),
                ),
                Text(
                  '${list[i].age}',
                  style: TextStyle(fontSize: 18.0, color: Colors.green),
                ),
                Text(
                  list[i].info,
                  style: TextStyle(fontSize: 16.0, color: Colors.blue),
                ),
              ],
            ),
          ));
}

//模拟生成模型数组
List<TestPerModel> getModelList(int count) {
  List<TestPerModel> list = [];
  for (var i = 0; i < count; i++) {
    list.add(TestPerModel('name$i', i, 'info$i'));
  }
  return list;
}

//定义类
class TestPerModel {
//  https://blog.csdn.net/s1120080286/article/details/83317798
  String name;
  int age;
  String info;

//  构造函数的几种写法

  TestPerModel(this.name, this.age, this.info);
  TestPerModel.info(this.name, this.age, info) {
    this.info = info;
  }

  TestPerModel.fromList(list) {
    this.name = list[0];
    this.age = list[1];
    this.info = list[2];
  }

  TestPerModel.fromListShort(list)
      : this.name = list[0],
        this.age = list[1],
        this.info = list[2];
}

class _ListViewState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ListView Show'),
      ),
      body: Center(
        child: ListView(
          children: getListWithCount(100),
//        children: [buildListViewUseBuilder(getModelList(100))],
        ),
      ),
    );
  }
}

//GridView 使用示例
List<Container> _buildGridTileList(int count) {
//  images/pic{index+1}.png;  { }  可以在字符串中操作
  return List<Container>.generate(
      count,
      (int index) => Container(
            child: Image.asset('images/002.png'),
          ));
}

Widget buildGrid() {
//  确定每行放置元素的个数
  return GridView.count(
    crossAxisCount: 2,
    padding: const EdgeInsets.all(4.0),
    mainAxisSpacing: 4.0,
    crossAxisSpacing: 4.0,
    children: _buildGridTileList(100),
  );

//  确定每行放置元素的宽度
//  return GridView.extent(
//    maxCrossAxisExtent: 150.0,
//    padding: const EdgeInsets.all(4.0),
//    mainAxisSpacing: 4.0,
//    crossAxisSpacing: 4.0,
//    children: _buildGridTileList(100),
//  );
}

class _GridViewState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
//    在网格中放置widget
//    检测列内容超过渲染框时自动提供滚动
//    构建您自己的自定义grid，或使用一下提供的grid之一:
//    GridView.count 允许您指定列数
//    GridView.extent 允许您指定项的最大像素宽度

    return Scaffold(
      appBar: AppBar(
        title: Text('Lalalalalala'),
      ),
      body: Center(
        child: buildGrid(),
      ),
    );
  }
}

//Container 使用示例
class _ContainerState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    var container = Container(
      decoration: BoxDecoration(
        color: Colors.black26,
      ),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Expanded(
                child: Container(
                  decoration: BoxDecoration(
                    border: Border.all(width: 10.0, color: Colors.black38),
                    borderRadius: BorderRadius.all(const Radius.circular(8.0)),
                  ),
                  margin: const EdgeInsets.all(4.0),
                  child: Image.asset('images/002.png'),
                ),
              ),
              Expanded(
                  child: Container(
                decoration: BoxDecoration(
                  border: Border.all(width: 10.0, color: Colors.black38),
                  borderRadius:
                      const BorderRadius.all(const Radius.circular(8.0)),
                ),
                margin: const EdgeInsets.all(4.0),
                child: Image.asset('images/002.png'),
              ))
            ],
          ),
          Row(
            children: <Widget>[
              Expanded(
                child: Container(
                  decoration: BoxDecoration(
                    border: Border.all(width: 10.0, color: Colors.black38),
                    borderRadius: BorderRadius.all(const Radius.circular(8.0)),
                  ),
                  margin: const EdgeInsets.all(4.0),
                  child: Image.asset('images/002.png'),
                ),
              ),
              Expanded(
                  child: Container(
                decoration: BoxDecoration(
                  border: Border.all(width: 10.0, color: Colors.black38),
                  borderRadius:
                      const BorderRadius.all(const Radius.circular(8.0)),
                ),
                margin: const EdgeInsets.all(4.0),
                child: Image.asset('images/002.png'),
              ))
            ],
          )
        ],
      ),
    );

    return Scaffold(
      appBar: AppBar(title: Text('This is a Test')),
      body: Container(
        child: container,
      ),
    );
  }
}

//行列嵌套
class _RowColumnState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
//    定义顶部的标题widget
    var titleText = Text(
      'Strawberry Pavlova',
      style: TextStyle(
        color: Colors.black,
        fontWeight: FontWeight.w900,
        fontFamily: 'Roboto',
        letterSpacing: 0.5,
        fontSize: 30.0,
      ),
    );

    var content =
        'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';

    //    定义文字内容widget
    var contentText = Text(
      content,
      textAlign: TextAlign.center,
      style: TextStyle(
        color: Colors.black,
        fontWeight: FontWeight.w900,
        fontFamily: 'Roboto',
        letterSpacing: 0.5,
        fontSize: 16.0,
        height: 1.4,
      ),
    );

//    顶部的星级和评论数widget
    var ratings = new Container(
      padding: new EdgeInsets.all(20.0),
      child: new Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Icon(
            Icons.star,
            color: Colors.black,
          ),
          Icon(
            Icons.star,
            color: Colors.black,
          ),
          Icon(
            Icons.star,
            color: Colors.black,
          ),
          Icon(
            Icons.star,
            color: Colors.black,
          ),
          Icon(
            Icons.star,
            color: Colors.black,
          ),
          new Text('170 Reviews',
              style: new TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.w800,
                fontFamily: 'Roboto',
                letterSpacing: 0.5,
                fontSize: 20.0,
              )),
        ],
      ),
    );

//    定义统一的文字样式c
    var descTextStyle = TextStyle(
      color: Colors.black,
      fontWeight: FontWeight.w800,
      fontFamily: 'Roboto',
      letterSpacing: 0.5,
      fontSize: 18.0,
      height: 2.0,
    );
    // DefaultTextStyle.merge可以允许您创建一个默认的文本样式，该样式会被其
    // 所有的子节点继承
    // 这是中间包含图标的文字的widget.
    var iconList = DefaultTextStyle.merge(
      style: descTextStyle,
      child: Container(
        padding: EdgeInsets.all(20.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            Column(
              children: <Widget>[
                Icon(
                  Icons.kitchen,
                  color: Colors.green[500],
                ),
                Text('PREP:'),
                Text('25 min'),
              ],
            ),
            Column(
              children: <Widget>[
                Icon(
                  Icons.kitchen,
                  color: Colors.green[500],
                ),
                Text('COOK:'),
                Text('1 hr'),
              ],
            ),
            Column(
              children: <Widget>[
                Icon(
                  Icons.kitchen,
                  color: Colors.green[500],
                ),
                Text('FEEDS:'),
                Text('4-6'),
              ],
            ),
          ],
        ),
      ),
    );

    var leftColumn = Container(
      padding: EdgeInsets.fromLTRB(20.0, 30.0, 20.0, 20.0),
      child: Column(
        children: <Widget>[
          titleText,
          contentText,
          ratings,
          iconList,
        ],
      ),
    );

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
//      body: Center(
//        child: Container(
//          margin: EdgeInsets.fromLTRB(0.0, 40.0, 0.0, 30.0),
//          height: 600.0,
////          child: Row(
////            crossAxisAlignment: CrossAxisAlignment.start,
////            children: <Widget>[
////              Container(
////                width: 440.0,
////                child: leftColumn,
////              )
////            ],
////          ),
//          child: leftColumn,
//        ),
//      ),
      body: ListView(
        children: <Widget>[
          Container(
            child: leftColumn,
          )
        ],
      ),
    );
  }
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.

//        行排列
//        child: Row(
//          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
//          children: <Widget>[
//            Expanded(child: Image.asset('images/001.png')),
//            Expanded(child: Image.asset('images/001.png')),
//            Expanded(child: Image.asset('images/001.png')),
//            Expanded(child: Image.asset('images/001.png')),
//          ],
//        ),

//      列排列
//        child: Column(
//          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
//          children: <Widget>[
//            Expanded(child: Image.asset('images/001.png')),
//            Expanded(child: Image.asset('images/001.png')),
//            Expanded(child: Image.asset('images/001.png')),
//            Expanded(child: Image.asset('images/001.png')),
//          ],
//        ),

//      聚集显示
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Icon(Icons.star, color: Colors.green[500]),
            Icon(Icons.star, color: Colors.green[500]),
            Icon(Icons.star, color: Colors.green[500]),
            Icon(Icons.star, color: Colors.black),
            Icon(Icons.star, color: Colors.black),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
